package com.appkubes.mall.admin.application.promoter.promoter.controller;

import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.mall.admin.application.promoter.promoter.entity.vo.PromoterAddVO;
import com.appkubes.mall.admin.application.promoter.promoter.entity.vo.PromoterSearchVO;
import com.appkubes.mall.admin.application.promoter.promoter.service.PromoterService;
import com.appkubes.mall.core.controller.BaseMallPromoterController;
import com.appkubes.mall.core.entity.MallPromoter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/promoter/")
@Api(tags = "推广员模块")
public class PromoterController extends BaseMallPromoterController {

    @Resource
    PromoterService promoterService;

    @ApiOperation("获取推广员统计信息")
    @GetMapping("/statis")
    public Response statis(@ParamValid PromoterSearchVO promoterSearchVO) {
        return Response.success(promoterService.statis(promoterSearchVO));
    }

    @ApiOperation("获取推广员列表")
    @GetMapping("/getList")
    public Response getList(@ParamValid PromoterSearchVO promoterSearchVO) {
        QueryWrapper<MallPromoter> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        if (!StringUtils.isEmpty(promoterSearchVO.getName())) {
            queryWrapper.like("promoter_name", promoterSearchVO.getName());
        }
        MallPromoter queryEntity = new MallPromoter();
        BeanUtils.copyProperties(promoterSearchVO, queryEntity);
        IPage<MallPromoter> promoterList = this.selectPageByEntity(queryEntity, promoterSearchVO);
        return Response.success(this.getBaseMapper().selectPage(promoterList, queryWrapper));
    }

    @ApiOperation("添加或修改推广员")
    @PostMapping("/save")
    public Response save(@ParamValid PromoterAddVO promoterAddVO) {
        MallPromoter promoter = new MallPromoter();
        BeanUtils.copyProperties(promoterAddVO, promoter);
        promoter.setCreateTime(DateUtils.getCurrentDate());
        Boolean flag = this.saveOrUpdate(promoter);
        return Response.success(flag);
    }
}
